<?php
class Application_Model_StoreLocation extends Application_Model_DomainObject{
	
	protected $_id;             //库位表的ID
	
	protected $_userId;        //用户ID
	
	protected $_locationNo;     //仓库号
        
        protected $_status;       //状态
        
        protected $_library;      //仓库列号
        
        protected $_datecreated;    //开库位时间
        
        public function getuserinfo(){
            if($this->_userId)
            {
                return Application_Model_User::load($this->_userId);
            }
        }
        
        public static function getSearch($name){
            $uwhere['( nikename like '] = "%".$name."%";
            $uwhere[ ' or storeno like '] = "%".$name."%";
            $uwhere[" ) and "] = 1;
            $res = Application_Model_User::getDao()->queryAll($uwhere);
            $or = "";
            $where = array();
            foreach($res as $val){
                $where[$or.' userId ='] = $val->id;
                $or = "or";
            }
            $where[$or.' locationNo like '] = "%".$name."%";
            return Application_Model_StoreLocation::getDao()->queryAll($where);
        }
        
        public static function getAllLocation($name){
            $where=array();
            if($name != null){
                $where=array('library'=>$name);
            }
            return Application_Model_StoreLocation::getDao()->queryAll($where);
        }
        
        public static function getUserAll($id)
        {
            return self::getDao()->queryAll(array("userId"=>$id));
        }
        
        public static function getRemoveLocation($locationNo)
        {
            $result = self::getDao()->queryOne(array("locationNo"=>$locationNo));
            if($result){
                $result->userId = null;
                $result->status = 0;
                $result->save();
                return 1;
            }else{
                return 0;
            }
        }
        public static function byUseridRemoveLocation($userId)
        {
            $result = self::getDao()->queryAll(array("userId"=>$userId));
            foreach ($result as $value) {
                $value->userId = null;
                $value->status = 0;
                $value->save();
            }
            return 1;
        }

        public static function getLocationInfo($locationNo)
        {
            return self::getDao()->queryOne(array("locationNo"=>$locationNo));
        }
        
        public static function getLocation($userId)
        {
            $res = self::getDao()->queryAll(array("userId"=>$userId));
            $storeNo = array();
            foreach($res as $val){
                $storeNo[] = $val->locationNo;
            }
            return $storeNo;
        }
        
       public function getParcelCount(){
           if($this->_status == 1){
               return Application_Model_StoreItem::getDao()->count(array('locationNo'=>$this->_locationNo,'interior'=>Application_Model_StoreItem::Interior_InStored." and (isdelete is null or isdelete = 0)"));       
           }   
       }
       
       public function emptyLocation($locationNo){
           $where = array();           
           $where['(interior = '] = Application_Model_StoreItem::Interior_InStored;
           $where[' or interior = '] = Application_Model_StoreItem::Interior_HoldItem;
           $where[') and (isdelete is null or isdelete = 0) and locationNo = '] = $locationNo;
           $mycount = Application_Model_StoreItem::getDao()->count($where);
           if($mycount == 0){
               $storelocation = self::getDao()->queryOne(array("locationNo"=>$locationNo));
               if($storelocation){
                    $opt = self::load($storelocation->id);
                    $opt->status = 0;
                    $opt->userId = NULL;
                    $opt->save();
               }
           }
       }
        
}